package servlets.admin;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import managers.EntityFunctionManager;

import entities.Event;
import entities.User;

/**
 * Servlet implementation class AdminFunctions.
 * Servlet generique qui recoit tous les fonctions d'un administrateur, selon besoin il appellera
 * aux methodes du manager
 */
public class AdminFunctions extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdminFunctions() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String choise = request.getParameter("choise");
		System.out.println(choise);
		if(choise.equals("valUser")) {
			getNonValidUsers(request,response);
		}
		if(choise.equals("banUser")) {
			System.out.println("banuser");
			getValidUsers(request,response);
		}
		if(choise.equals("valEvent")) {
			getNonValidEvents(request,response);
		}
		if(choise.equals("logOut")) {
			logOut(request,response);
		}
		if(choise.equals("cat")) {
			getCategory(request,response);
		}
		
	}
	
	/**
	 * Get non valid users
	 * @throws IOException 
	 * @throws ServletException 
	 */
	@EJB EntityFunctionManager mng;
	protected void getNonValidUsers(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<User> list = mng.listUsersNonValide();
		String goTo="ListNewUsers.jsp";
		String myObjectId = UUID.randomUUID().toString();	
		request.getSession().setAttribute(myObjectId, list);
		request.setAttribute("data", list);
		request.getRequestDispatcher(goTo).forward(request, response);
	}
	
	/**
	 * Gets all valid users.
	 */

	protected void getValidUsers(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		List<User> list = mng.listUsersValide();
		String goTo="ListValidUsers.jsp";
		String myObjectId = UUID.randomUUID().toString();	
		request.getSession().setAttribute(myObjectId, list);
		request.setAttribute("data", list);
		request.getRequestDispatcher(goTo).forward(request, response);
	}
	
	/**
	 * Gerer les categories
	 * @throws IOException 
	 * @throws ServletException 
	 */
	
	protected void getCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		String goTo="toSevCategory.jsp";
		request.getRequestDispatcher(goTo).forward(request, response);
	}
	
	/**
	 * Get non valide events
	 */
	
	protected void getNonValidEvents(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		List<Event> list = mng.listNewEvents();
		String goTo="NewEvents.jsp";
		String myObjectId = UUID.randomUUID().toString();	
		request.getSession().setAttribute(myObjectId, list);
		request.setAttribute("data", list);
		request.getRequestDispatcher(goTo).forward(request, response);
	}
	
	/**
	 * We can log out of the system.
	 */
	protected void logOut(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException  {
		 try {
	            // on récupère la session ou on la crée si elle n'existe pas
	           // HttpSession session = request.getSession(true);
	            // on l'invalide et on revient sur la page d'accueil
	            request.getSession(false).invalidate();
	            //session.removeAttribute("id");
	            request.setAttribute("message","Logout successed");
	           
	            request.getRequestDispatcher("loginAdmin.jsp").forward(request,response);
	        } catch (Exception e) {
	            request.setAttribute("error",e);
	            request.setAttribute("message","Logout failed");
	            request.getRequestDispatcher("loginAdmin.jsp").forward(request,response);
	        }
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request,response);
	}

}
